package com.sakura.mybatis;

import com.sakura.mybatis.builder.Resources;
import com.sakura.mybatis.builder.SqlSessionFactoryBuilder;
import com.sakura.mybatis.entity.UserDO;
import com.sakura.mybatis.factory.SqlSessionFactory;
import com.sakura.mybatis.mapper.UserMapper;
import com.sakura.mybatis.session.SqlSession;

import javax.xml.parsers.ParserConfigurationException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * @author wangxinyuchuan
 * @version MybatisContextLoader.java v1.0 2025/4/15 15:41 wangxinyuchuan Exp $
 */
public class MybatisContextLoader {
    public static void main(String[] args) throws ParserConfigurationException {
        InputStream resource = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryBuilder.build(resource);
//        System.out.println("加载sqlSessionFactory结束");
        SqlSession sqlSession = sqlSessionFactory.openSession();

        UserDO userDO = new UserDO();
        userDO.setAge(18);
        userDO.setUserName("donk");
        userDO.setId(1L);
        userDO.setCreatedTime(new Date());

//        List<UserDO> users = sqlSession.selectList("com.sakura.mybatis.mapper.UserMapper.listAllUser", null);
//        System.out.println(users);

//        UserDO user = sqlSession.selectOne("com.sakura.mybatis.mapper.UserMapper.selectUserByUserNameAndAge", userDO);
//        System.out.println(user);

        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
//        List<UserDO> userDOS = userMapper.listAllUser();
//        System.out.println(userDOS);

        UserDO user = userMapper.selectUserByUserNameAndAge(userDO);
        System.out.println(user);

        sqlSession.close();
    }
}
